using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : Character
{
    #region Info Of Player
    [Header("Move Info")]
    public float moveSpeed;
    private float defaultMoveSpeed;
    public float runSpeedMul;

    public float jumpForce;
    public int maxJumpCount;
    [HideInInspector] public int currentJumpCount;
    private float defaultJumpForce;
    [SerializeField] public float fallGravity;
    [HideInInspector] public float defaultGravity;

    //public float evadeSpeed;            
    //public float evadeTime;             

    private float lastTimeLeftPress;
    private float lastTimeRightPress;
    private float maxWaitTime = 0.2f;

    public bool isEvading;

    [Header("Attack Info")]
    public bool isBusy;
    public float[] attackMovement;

    public WeaponData currentWeapon;
    public int currentWeaponIndex;
    public List<WeaponData> weaponInEquiped;

    public System.Action onCurrentWeaponChanged;

    private SkillManager skillManager;

    public SkillEnum currentSkill;
    #endregion

    #region PlayerStates

    public PlayerStateMachine stateMachine { get; private set; }

    public PlayerIdleState idleState { get; private set; }
    public PlayerMoveState moveState { get; private set; }
    public PlayerJumpState jumpState { get; private set; }
    public PlayerAirState airState { get; private set; }
    public PlayerDeadState deadState { get; private set; }
    public PlayerPriAtkState attackState { get; private set; }

    #endregion

    protected override void Awake()
    {
        base.Awake();

        stateMachine = new PlayerStateMachine();

        #region InstantiationState

        idleState = new PlayerIdleState(this, stateMachine, "Idle");
        moveState = new PlayerMoveState(this, stateMachine, "Move");
        jumpState = new PlayerJumpState(this, stateMachine, "Jump");
        airState = new PlayerAirState(this, stateMachine, "Jump");
        deadState = new PlayerDeadState(this, stateMachine, "Dead");
        attackState = new PlayerPriAtkState(this, stateMachine, "Attack");

        #endregion

        skillManager = GetComponent<SkillManager>();
        skillDataBase = skillManager.skillDataBase;
    }

    protected override void Start()
    {
        base.Start();

        stateMachine.Initialize(idleState);

        defaultMoveSpeed = moveSpeed;
        defaultJumpForce = jumpForce;
        defaultGravity = rb.gravityScale;

        lastTimeLeftPress = lastTimeRightPress = -maxWaitTime;
    }

    protected override void Update()
    {
        base.Update();

        stateMachine.currentState.Update();         

        if (Input.GetKeyDown(KeyCode.Q))
            SwitchCurrentWeapon();

        if(Input.GetKeyDown(KeyCode.K))
        {
            skillManager.ReleaseSkill(currentSkill);
        }

        //if (Input.GetKeyDown(KeyCode.LeftShift))
        //stateMachine.ChangeState(evadeState);

        //RunCheck();
    }

    #region Attack Detail Function

    public IEnumerator BusyFor(float _seconds)    
    {
        isBusy = true;          

        yield return new WaitForSeconds(_seconds);

        isBusy = false;
    }

    private void RunCheck()
    {
        if (Input.GetKeyDown(KeyCode.A))
        {
            if (Time.time - lastTimeLeftPress < maxWaitTime)
            {
                //stateMachine.ChangeState(runState);
            }
            lastTimeLeftPress = Time.time;
        }

        if (Input.GetKeyDown(KeyCode.D))
        {
            if (Time.time - lastTimeRightPress < maxWaitTime)
            {
                //stateMachine.ChangeState(runState);
            }
            lastTimeRightPress = Time.time;
        }
    }

    private void SwitchCurrentWeapon()
    {
        currentWeaponIndex++;

        if(currentWeaponIndex > weaponInEquiped.Count - 1) 
        {
            currentWeaponIndex = 0;
        }

        currentWeapon = weaponInEquiped[currentWeaponIndex];

        if (onCurrentWeaponChanged != null)
            onCurrentWeaponChanged();
    }

    public void AnimationTrigger() => stateMachine.currentState.AnimationFinishTrigger();

    public void Die()
    {

    }
    #endregion
}
